﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.IO.Compression;

namespace OnlyleHttpModel
{
    public class CompressWhitespaceFilter : Stream
    {
        private GZipStream _contentGZipStream;
        private DeflateStream _content_DeflateStream;
        private Stream _contentStream;
        private CompressOptions _compressOptions;

        public CompressWhitespaceFilter(Stream contentStream, CompressOptions compressOptions)
        {
            if (compressOptions == CompressOptions.GZip)
            {
                this._contentGZipStream = new GZipStream(contentStream, CompressionMode.Compress);
                this._contentStream = this._contentGZipStream;
            }
            else if (compressOptions == CompressOptions.Deflate)
            {
                this._content_DeflateStream = new DeflateStream(contentStream, CompressionMode.Compress);
                this._contentStream = this._content_DeflateStream;
            }
            else
            {
                this._contentStream = contentStream;
            }
            this._compressOptions = compressOptions;
        }
        public override bool CanRead
        {
            get { return this._contentStream.CanRead; }
        }
        public override bool CanSeek
        {
            get { return this._contentStream.CanSeek; }
        }
        public override bool CanWrite
        {
            get { return this._contentStream.CanWrite; }
        }
        public override void Flush()
        {
            this._contentStream.Flush();
        }
        public override long Length
        {
            get { return this._contentStream.Length; }
        }
        public override long Position
        {
            get
            {
                return this._contentStream.Position;
            }
            set
            {
                this._contentStream.Position = value;
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return this._contentStream.Read(buffer, offset, count);
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return this._contentStream.Seek(offset, origin);
        }
        public override void SetLength(long value)
        {
            this._contentStream.SetLength(value);
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] data = new byte[count + 1];
            Buffer.BlockCopy(buffer, offset, data, 0, count);
            string strtext = System.Text.Encoding.UTF8.GetString(data);
            strtext = Regex.Replace(strtext, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            strtext = Regex.Replace(strtext, "\\r\\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            strtext = Regex.Replace(strtext, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
            byte[] outdata = System.Text.Encoding.UTF8.GetBytes(strtext);
            this._contentStream.Write(outdata, 0, outdata.GetLength(0));
        }
    }
    public enum CompressOptions
    {
        GZip,
        Deflate,
        None
    } 
}
